<template>
  <div class="holesTable">
    <table cellspacing="0"
      border-collapse="0">
      <template
        v-for="(item, index) in tableData">
        <tr align="center" v-if="index === 0"
          :key="index">
          <td class="label"></td>
          <td v-for="head in 12" :key="head"
            class="label">
            {{head}}
          </td>
        </tr>
      </template>
      <tr align="center"
        v-for="(item, index) in tableData"
        :key="index">
        <template v-if="item.row">
          <td class="label">{{item.row}}</td>
          <td v-for="(head, i) in 12"
            :key="head">
            <el-input
              v-if='JSON.stringify(ngList) === "{}"'
              :disabled="!noDisabled.includes(`${item.row}${i+1}`)"
              v-model="item.content[`${item.row}${i+1}`]" />
            <div 
              v-else
              class="text_item"
              :style="setYinyang(`${item.row}${i+1}`)"
              :class="{
                active: ngList[`${item.row}${i+1}`] >= 0 ? false : true,
                computed: unKnowResult && unKnowResult[`${item.row}${i+1}`] ? true : false,
              }">
              {{ ngList[`${item.row}${i+1}`] || '' }}{{Symbol}}
            </div>
          </td>
        </template>
      </tr>
    </table>
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableData: [
        {
          row: "A",
          content: {},
        },
        {
          row: "B",
          content: {},
        },
        {
          row: "C",
          content: {},
        },
        {
          row: "D",
          content: {},
        },
        {
          row: "E",
          content: {},
        },
        {
          row: "F",
          content: {},
        },
        {
          row: "G",
          content: {},
        },
        {
          row: "H",
          content: {},
        },
      ],
      result: [],
    };
  },
  props: {
    yinYangHole: {
      type: Boolean,
      default: false
    },
    // 填写孔号的数组
    noDisabled: {
      type: Array,
      default: () => [],
    },
    // 自己填的数据 需要回显的数据
    exportConcentration: {
      type: Object,
      default: () => {},
    },
    // 计算出来的结果，用于区分背景色
    unKnowResult: {
      type: Object,
      default: () => {}
    },
    // 清空表格数据
    clear: {
      type: Boolean,
      default: false
    },
    // 是否拼接特殊单位
    Symbol: {
      type: String,
      default: ''
    }
  },
  watch: {
    tableData: {
      handler(value) {
        this.handleChange(value);
      },
      deep: true,
    },

    // 重置表格数据
    clear: {
      handler(val) {
        this.$emit('update:clear', false)
        if(!val) return
        this.$nextTick(() => {
          this.tableData.forEach(item => {
            item.content = {}
          })
        // this.result = []
        })
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
    ngList() {
      const obj = {};
      for (const k in this.exportConcentration) {
        obj[k] = this.exportConcentration[k]+ '';
      }
      return obj;
    },
  },
  methods: {
    setYinyang(hole) {
      if(!this.yinYangHole) return {}
      return { 
        background: this.yinYangHole && this.ngList[hole] == 0 ? '#6cbe6c' : '#ff8080' ,
        color: '#fff'
      }
    },
    handleChange(value) {
      let keysList = []
      this.noDisabled.forEach((item, index) => {
        value.forEach((sub) => {
          for (const k in sub.content) {
            if (item === k) {
              keysList[index] = item
              this.result[index] = sub.content[k];
            }
          }
        });
      });

      const resultList = Array.from(this.result);
      const data = resultList.map((item) => item || "");
      this.$emit("inputHoles", data, keysList);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .el-input__inner {
    width: 100%;
    height: 40px;
    outline-color: #409eff;
    padding: 0 2px;
    border-radius: inherit;
    text-align: center;
  }
  .el-input.is-disabled .el-input__inner {
    background-color: #dbf3ff;
  }
}
.holesTable {
  width: 100%;
  table {
    border-width: 0.1px;
    border-color: #b9cede;
    tr {
      .label {
        min-width: 60px;
        height: 40px;
        background-color: #409eff;
        color: #fff;
      }
      td {
        box-sizing: border-box;
        width: 60px;
        border-color: #b9cede;
        border: 1px solid #b9cede;
        input {
          width: 100%;
          height: 38px;
          border: none;
          outline-color: #409eff;
        }
        .text_item {
          height: 40px;
          line-height: 40px;
          color: #000;
        }
      }
    }
  }
  .active {
    background-color: #dbf3ff !important;
    height: 40px;
    line-height: 40px;
    cursor: not-allowed;
  }
  .computed {
    background-color: #89afe0;
    height: 40px;
    line-height: 40px;
  }
  .yinYang {
    background-color: pink;
  }
}
</style>